
<template>
  <div class="container">
    <a-space direction="vertical" style="width: 100%" :size="16">
      <a-card style="width: 100%" title="售后原因配置">
        <a-form :model="formState">
          <a-row :gutter="24">
            <a-col :span="6">
              <a-form-item label="售后原因">
                <a-input
                  style="width: 100%"
                  v-model:value="formState.searchCondition"
                  placeholder="输入类目名称"
                />
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="状态">
                <a-select
                  ref="select"
                  v-model:value="formState.state"
                  style="width: 100%"
                  placeholder="状态"
                >
                  <a-select-option :value="0">全部</a-select-option>
                  <a-select-option :value="1">启用</a-select-option>
                  <a-select-option :value="2">禁用</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>

        <div class="layout-flexSpace">
          <a-space wrap>
            <a-button type="primary" @click="handleSearch">查询</a-button>
            <a-button @click="handleReset">重置</a-button>
          </a-space>
          <a-space wrap>
            <a-button type="primary" @click="handelOpen({})">新建</a-button>
          </a-space>
        </div>
      </a-card>

      <a-card>
        <a-table
          :dataSource="dataSource"
          :columns="afterSaleColumns"
          :pagination="pagination"
          @change="handleChangeSize"
        >
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'state'">
              {{ record.state == 1 ? "启用" : "禁用" }}
            </template>
            <template v-if="column.key === 'operate'">
              <a-space>
                <a-button type="primary" @click="handelOpen(record)"
                  >编辑</a-button
                >
                <a-popconfirm
                  :title="`您是否要${
                    record.state == 2 ? '启用' : '禁用'
                  }此条数据?`"
                  @confirm="handleStateChange(record)"
                >
                  <a-button v-if="record.state == 1" danger type="primary"
                    >禁用</a-button
                  >
                  <a-button v-else type="primary"
                    >启用</a-button
                  >
                </a-popconfirm>
                <a-popconfirm
                  title="您是否要删除此条数据?"
                  ok-text="是"
                  cancel-text="否"
                  @confirm="confirm(record.id)"
                >
                  <a-button danger type="primary">删除</a-button>
                </a-popconfirm>
              </a-space>
            </template>
          </template>
        </a-table>
      </a-card>
    </a-space>
    <afterSaleModal ref="hotCakesRef" @refreshList="refreshList" />
  </div>
</template>
<script setup>
import { ref, onMounted } from "vue";
import { afterSaleColumns } from "./columns.js";
import afterSaleModal from "./components/afterSaleModal.vue";
import {
  getReturnReason,
  saveReturnReason,
  delReturnReason,
} from "@/api/sysConfig";
import { message } from "ant-design-vue";
const pagination = ref({
  current: 1,
  pageSize: 10,
  defaultPageSize: 10,
  total: 0,
  showSizeChanger: true,
  pageSizeOptions: ["10", "20", "50", "100"],
  showTotal: () => `共 ${pagination.value.total} 条`,
});
const formState = ref({
  page: pagination.value.current,
  pageSize: pagination.value.pageSize,
  searchCondition: undefined,
  state: undefined,
  type: 1
});
const hotCakesRef = ref(null);
// 售后原因列表
const dataSource = ref([]);
function getReturnReasonList() {
  getReturnReason(formState.value).then((res) => {
    if (res.code === 200) {
      dataSource.value = res.data.list.filter(val => val.state !=3);
      pagination.value.total = res.data.total;
    }
  });
}
function handleSearch() {
  formState.value.page = 1
  formState.value.pageSize = 10
  getReturnReasonList();
}
const handelOpen = (record) => {
  hotCakesRef.value.open(record);
};

const confirm = (id) => {
  delReturnReason({ id }).then((res) => {
    if (res.code === 200) {
      message.success("删除成功");
      getReturnReasonList();
    }
  });
};
function handleReset() {
  formState.value = {
    page: 1,
    pageSize: 10,
    searchCondition: undefined,
    state: undefined,
    type: 1
  };
  pagination.value.current = 1
  pagination.value.pageSize = 10
  getReturnReasonList();
}
function handleChangeSize(page) {
  formState.value.page = page.current;
  formState.value.pageSize = page.pageSize;
  pagination.value.current = page.current;
  pagination.value.pageSize = page.pageSize;
  getReturnReasonList();
}
// 启用禁用
function handleStateChange(row) {
  saveReturnReason({ type: 1, id: row.id, state: row.state == 1 ? 2 : 1 }).then(
    (res) => {
      if (res.code === 200) {
        getReturnReasonList();
      }
    }
  );
}
function refreshList() {
    getReturnReasonList();
}
onMounted(() => {
  getReturnReasonList();
});
</script>

<style lang="less">
.img-item {
  display: flex;
  flex-direction: column;
  align-items: center;

  .img-label {
    margin-top: 6px;
    color: #9599a5;
    font-size: 12px;
  }
}
</style>
